package com.dotflat.pay.controller;

import com.google.common.collect.ImmutableMap;
import com.starinside.wp.domain.Children;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.service.ChildrenService;
import com.starinside.wp.utils.RemoteCallUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Slf4j
@RestController
@RequestMapping("/v2/pay/wallet")
@Api(tags = "缴费 - 钱包（余额，总消费金额，总充值金额）")
public class WalletController {

    @Value("${server.crm.url}")
    private String crmServerURL;

    @Autowired
    private ChildrenService childrenService;

    @GetMapping
    public Object getWallet(HttpServletRequest req,
                               @RequestHeader(TICKET) String ticket) {

        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        BindedBabyVO baby = user.getDefaultBaby();
        if (baby == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "宝宝不存在");
        }

        Children c = childrenService.getById(baby.getId());

//        areaId, String clientId
        Map p = ImmutableMap.of("areaId", baby.getAreaId(), "clientId", c.getCrmClienteleId());

        //先添加CRM客户
        HashMap result = RemoteCallUtil.call(crmServerURL + "/api/other_system/wallet", p, null, HashMap.class, "获取钱包数据失败");

        return StatusResponse.success(result);
    }
}
